
import pandas
import numpy

# 每组30个, 分批计算误差
y = [23, 323, 323,434,34, 1, 0, 3]
y_true = [24, 323, 323,434,34, 0, 1, 3]
batch = 3
acc_total = 0
acc_batch = []
batch_acc = 0
for i in range(len(y)):
    if y[i] == y_true[i]:
        acc_total = acc_total + 1
        batch_acc = batch_acc + 1
    if i % batch == 0:
        if i != 0:
            acc_batch.append(batch_acc)
        batch_acc = 0

print("\033[34m预测的值:", y)
print("\033[32m实际的值:", y_true)

分批准确率 = ""
for i in range(len(acc_batch)):
    分批准确率 = 分批准确率 + str(acc_batch[i]/batch * 100) + "%, "
print("\033[33m分批准确率:", 分批准确率)
print("\033[33m总准确率:", acc_total/len(y) * 100, "%")

error = []
batch = 3
abs_error_batch = []
for i in range(len(y)):
    error.append(abs(y[i] - y_true[i]))
    if i % batch == 0 or i == len(y) - 1:
        if i != 0:
            abs_error_batch.append(error_batch)
        error_batch = 0
    error_batch = error_batch + abs(y[i] - y_true[i])

print("\033[34m预测的值:", y)
print("\033[32m实际的值:", y_true)
print("\033[32m绝对值误差:", error)

分批平均绝对误差 = ""
for i in range(len(abs_error_batch)):
    分批平均绝对误差 = 分批平均绝对误差 + str(abs_error_batch[i] / len(batch) * 100) + "%, "
print("\033[33m分批平均绝对误差:", 分批平均绝对误差)
print("\033[33m总平均绝对误差:", error / len(y) * 100, "%")

# 定义类, 封装属性和方法, 方便继承
class Fruit:
    # 类变量, 所有实例共享的, 通过 类名.变量名 访问
    name = "水果"
    # 保护类变量, 只有自己和子类可以访问
    _name = "水果"
    # 两个下划线开头, 私有类变量, 只有类内部的方法可以访问
    __name = "水果"
    # 构造方法 __init__(self), self指类的实例, 必须声明, 但调用时不必传入, 当创建了这个类的实例时就会调用该方法
    def __init__(self):
        # 类变量
        Fruit.name = "水果"
        # 实例变量
        self.color = "red"

# 继承父类fruit, 可继承公有和保护属性和方法, 可重写父类方法
class Apple(Fruit):
    # 类变量
    name = "苹果"
    # 构造方法, 如果在子类中需要父类的构造方法就需要显式的调用父类的构造方法，或者不重写父类的构造方法
    def __init__(self):
        self.color = "red"
        # 调用父类方法, 类名.方法名(self), self必传
        Fruit.__init__(self)
        # 调用私有方法 self.方法名
        self.__private_mothod()
    # 私有方法, 只能在类内部调用, 箭头后面声明返回值类型, 可以不填, 用于提醒调用方
    def __private_mothod(self) -> str:
        pass

# 创建实例
apple = Apple()
# 访问类变量
applename = Apple.name
# 修改实例变量
apple.color = "xxx"

a, b  = [1,1, 2, 2, 2, 3, 2, 5, 7,1], [[1, 5, 7], [3,5,7,8]]
dataFile = open("temp/data/通信达/test.csv", 'w+')
dataFile.writelines("a,b,[1],[2]")
dataFile.close()

dataF = {"Student": ["Kamal", "Arun", "David", "Thomas", "Steven"],
          "Economics": [10, 8, 6, 5, 8],
          "Fine Arts": [7, 8, 5, 9, 6],
          "Mathematics": [7, 3, 5, 8, 5]}
dataFrame = pandas.DataFrame(dataF)
dataFrame.to_csv("temp/data/通信达/test.csv", index=False, sep=";")

d = {'a': [1], 'b': [1, 2], 'c': [], 'd':[2]}
for key in d:
    if len(d[key]) != len(d['a']):
        del d[key]
k=[0,0,0,0,0,0,0,0,0,0,0,0,0]
kk = sorted(set(a).union(set(k)))
for j in range(len(a)):
    # 剔除重复数据 - -!
    if len(numpy.where(numpy.asarray(a[0:j]) == a[j])[0]) > 0:
        continue
    # dsa
    k[j] = a[j]
#
# for i in range(len(a)):
#     for j in range(len(b)):
#         break

for j in range(len(b)):
    oldChannel = b[j]
    for i in range(len(a)):
        # 下面方法太慢
        # try:
        #     # numpy.where(numpy.asarray(oldChannel, dtype='l')[:, -1]==1612362600)
        #     list(numpy.asarray(oldChannel, dtype='l')[:, -1]).index(a[i])
        # except BaseException:
        #     oldChannel.insert(i, [0,0,0,0,0,0,0,0,0])
        for k in range(i, len(oldChannel)):
            if a[i] == oldChannel[k]:
                break
            if oldChannel[k] > a[i]:
                oldChannel.insert(i, 0)
                break

c = a
a.insert(1,"p")
c = set(a).union(set(b))
set(c)
try:
    index = a.index(333)
except BaseException:
    print(c)
else:
    print(c)
print(c)